#!/usr/bin/python 
#   yongming.li

import os
import  sys
mylog = 'lmdd_results.log'

lmddPath = os.path.dirname(__file__)
print 'lmddPath is : ' + lmddPath
sys.path.append('../'+lmddPath)

def run(cmd,index):
        regsfile = os.popen(cmd).readlines()
        for  line  in regsfile: 
            #if len (line) == 0  or  len (line) <= 3:  # Zero length indicates EOF 
            break  
cmds=[#'adb shell stop ',
     #'adb shell sleep 1',
      #'rm '+ mylog,
      #'adb shell cat /proc/cpuinfo >> '+mylog,
      'adb remount',
      #'adb push ./lmdd  /data',
      'adb push '+ lmddPath+'/lmdd  /data',
      'adb shell chmod 777 /data/lmdd'
      ]

def testEMMC(testtype,datasize,loops,results,finallyResult):
    cmdsize= len(cmds)
    for index in range(0,cmdsize):
        run(cmds[index],index); 
    #os.popen('echo \"test %s ..........................................................................................................................................................................................................................\" >> '%(type)+mylog)

    J=datasize
    #for J in ('4k','8k','16k','32k','64k','128k','256k','512k','1m','2m'):
    for J in (datasize,''):
        if J=='':
            break
        print 'datasize is '+datasize
        if    testtype=='read':
              precmd ='adb  shell  "echo 3 > /proc/sys/vm/drop_caches" '
              #cmd = 'adb shell /data/lmdd if=/data/dumb of=internal move='+J+' fsync=1 >>'+' '+mylog
              cmd = 'adb shell /data/lmdd if=/data/dumb of=internal move='+J+' fsync=1 '
        if    testtype=='write':
              precmd = ''
              #cmd = 'adb shell /data/lmdd if=internal of=/data/dumb move='+J+' fsync=1 >>'+' '+mylog
              cmd = 'adb shell /data/lmdd if=internal of=/data/dumb move='+J+' fsync=1 '
        print 'cmd is' +cmd
        # test result like this
        # 2.0972 MB in 0.0320 secs, 65.6303 MB/sec
        #os.popen('echo '+J+' >> '+mylog)
        averageSum=0
        for i in range (0,loops):
            if precmd != '':
               lines=os.popen(precmd).readlines()
            lines=os.popen(cmd).readlines()
            for line in lines:
                #results.extend(lines)
                item = line.split()
                line = line.strip()
                print line
                results.append(J+' : '+item[5]+'  '+item[6])
                averageSum=averageSum+float(item[5])
        averageSum = (averageSum/loops)
        results.append(J+' : test %d times average is %f'%(loops,averageSum))
        finallyResult[J]=averageSum
        print averageSum
            
    #print results
       
def startEMMC(tpye,results,finallyResult):
    
    testEMMC(tpye,results,finallyResult)  
    #os.popen('echo \"test write .........................................................................................................................................................................................................................\"  >> '+mylog)
    #testEMMC(tpye) 

if __name__ == '__main__':
    startEMMC()
